﻿using AtomUI.Controls;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Styling;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SD.App.Desktop
{
    public class MessageService : IMessageService, ISingletonLifetime
    {
        // 获取主窗口（TopLevel），弹窗挂载的父容器
        private TopLevel? GetMainTopLevel()
        {
            if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                return desktop.MainWindow as TopLevel;
            }
            throw new InvalidOperationException("无法获取主窗口（TopLevel），无法显示弹窗");
        }

        #region 普通消息提示（自动关闭）
        public void ShowMessage(string message, int duration = 2000)
        {
            ShowMessageInternal(message, MessageType.Information, duration);
        }

        public void ShowSuccess(string message, int duration = 2000)
        {
            ShowMessageInternal(message, MessageType.Success, 2500);
        }

        public void ShowError(string message, int duration = 2000)
        {
            ShowMessageInternal(message, MessageType.Error, 3500);
        }

        // 消息提示核心逻辑：创建 MessageControl
        private void ShowMessageInternal(string message, MessageType type, int duration)
        {
            var topLevel = GetMainTopLevel();
            if (topLevel == null) return;
             
            var _messageManager = new WindowMessageManager(topLevel)
            {
                MaxItems = 10
            };

            _messageManager?.Show(new Message(
                type: type,
                content: message,
                expiration: TimeSpan.FromMilliseconds(duration)
            ));
        }
        #endregion

    }
}
